let Storage = {}
const USEOPENID = "use"
const REDPKG = "redPkg"
const EDITADDR = "editAddr"
const TIP = "tip"
const ORDER = "order"
const SELADDR = "selAddr"
const ORDERDETAIL = "orderDetail"
const ORDERLIST = "orderList"
const PID = "pid"
const CLKID = "clk_id"
const FROM = "from"
const APPID = "appid"
const CNZZID = "c_id"
const CNZZWBID = "c_web_id"

const TTIP = "ttip"
const QZGDT = "qz_gdt"
const VERSION = "version"
const STARTTIME= "start_time"
const CONTENTID = "content_id";
const ADGROUPID = "adgroup_id";
const CAMPAIGN = "campaign_id";
const PHONE = "cur_phone"
const ALIBOX = "ali_d_b"
const ALIAPP = "ali_app"
const COUPONNUM = "serial_number"
const QUERY = "q_r"
const ADDR = "addr"

Storage.setAddr = (obj)=>{
	set(ADDR,obj)
}
Storage.getAddr = ()=>{
	return get(ADDR)
}
Storage.setQuery = (obj)=>{
	let data = {
		time:new Date(),
		val:obj
	}
	set(QUERY,data)
}
Storage.getQuery = ()=>{
	var obj = obj = get(QUERY)
	if(obj){
		var old_time = format_date(new Date(obj.time));
		var now_time = get_endtime();
		var compare_res = compare_time(old_time)
		if(compare_res){
			return obj.val
		}else{
			remove(QUERY);
			return ""
		}
	}else{
		remove(QUERY);
		return ""
	}
}


Storage.setCouponNum = (obj)=>{
	set(COUPONNUM,obj)
}
Storage.getCouponNum = ()=>{
	return get(COUPONNUM)
}

Storage.setIsAliApp = (obj)=>{
	set(ALIAPP,obj)
}
Storage.getIsAliApp = ()=>{
	return get(ALIAPP)
}
Storage.setAliDialogBox = (obj)=>{
	set(ALIBOX,obj)
}
Storage.getAliDialogBox = ()=>{
	return get(ALIBOX)
}

Storage.setPHONE = (obj)=>{
	set(PHONE,obj)
}
Storage.getPHONE = (obj)=>{
	return get(PHONE)
}

Storage.setCONTENTID = (obj)=>{
	set(CONTENTID,obj)
}
Storage.getCONTENTID = (obj)=>{
	return get(CONTENTID)
}
Storage.setADGROUPID = (obj)=>{
	set(ADGROUPID,obj)
}
Storage.getADGROUPID = (obj)=>{
	return get(ADGROUPID)
}
Storage.setCAMPAIGN = (obj)=>{
	set(CAMPAIGN,obj)
}
Storage.getCAMPAIGN = (obj)=>{
	return get(CAMPAIGN)
}

Storage.setSTARTTIME = (obj)=>{
	set(STARTTIME,obj)
}
Storage.getSTARTTIME = (obj)=>{
	return get(STARTTIME)
}
Storage.remSTARTTIME = (obj)=>{
	remove(STARTTIME)
}

Storage.setVERSION = (obj)=>{
	set(VERSION,obj)
}
Storage.getVERSION = (obj)=>{
	return get(VERSION)
}
Storage.remVERSION = (obj)=>{
	remove(VERSION)
}

Storage.setQZGDT = (obj)=>{
	let data = {
		time:new Date(),
		val:obj
	}
	set(QZGDT,data)
}
Storage.getQZGDT = ()=>{
	var obj = get(QZGDT)	
	if(obj){
		var old_time = format_date(new Date(obj.time));
		var now_time = get_endtime();
		var compare_res = compare_time(old_time)
		if(compare_res){
			return obj.val
		}else{
			remove(QZGDT);
			return ""
		}
	}else{
		remove(QZGDT);
		return ""
	}
	
}

Storage.setTTIP = (obj)=>{
	set(TTIP,obj)
}
Storage.getTTIP = (obj)=>{
	return get(TTIP)
}

Storage.setSCENE = (obj)=>{
	set(SCENE,obj)
}
Storage.getSCENE = ()=>{
	return get(SCENE)
}

Storage.setCNZZWBID = (obj)=>{
	set(CNZZWBID,obj)
}
Storage.getCNZZWBID = ()=>{
	return get(CNZZWBID)
}
Storage.setCNZZID = (obj)=>{
	set(CNZZID,obj)
}
Storage.getCNZZID = ()=>{
	return get(CNZZID)
}

Storage.setAPPID = (obj)=>{
	set(APPID,obj)
}
Storage.getAPPID = ()=>{
	return get(APPID)
}
Storage.setFrom = (obj)=>{
	set(FROM,obj)
	
}
Storage.getFrom = ()=>{
	return get(FROM)
}
Storage.setClkId = (obj)=>{
	let data = {
		time:new Date(),
		val:obj
	}
	set(CLKID,data)
	
}
Storage.getClkId = ()=>{
	var obj = obj = get(CLKID)
	if(obj){
		var old_time = format_date(new Date(obj.time));
		var now_time = get_endtime();
		var compare_res = compare_time(old_time)
		if(compare_res){
			return obj.val
		}else{
			remove(CLKID);
			return ""
		}
	}else{
		remove(CLKID);
		return ""
	}
	
}
Storage.setPID = (obj)=>{
	set(PID,obj)
}
Storage.getPID = ()=>{
	return get(PID)||'1'
}
Storage.getOrderList = ()=>{
	return get(ORDERLIST) || []
}
Storage.setOrderDetail = (obj)=>{
	let orderList = get(ORDERLIST) || []
	obj.id = orderList.length + 1
	orderList.push(obj)
	set(ORDERLIST, orderList)
	set(ORDERDETAIL,obj)
}
Storage.getOrderDetail = ()=>{
	return get(ORDERDETAIL)
}
Storage.setSelAddr = (obj)=>{
	set(SELADDR,obj)
	
}
Storage.getSelArrd = ()=>{
	return get(SELADDR)
	
}
Storage.setOrder = (obj)=>{
	set(ORDER,obj)
}
Storage.getOrder = ()=>{
	return get(ORDER)
}
Storage.removeOrder = ()=>{
	remove(ORDER)
}
Storage.setUse = (obj)=>{
	let data = {
		time:new Date(),
		val:obj
	}
	set(USEOPENID,data)	
}
Storage.getUse = ()=>{
	var obj = obj = get(USEOPENID)
	if(obj){
		var old_time = format_date(new Date(obj.time));
		var now_time = get_endtime();
		var compare_res = compare_time(old_time,7)
		if(compare_res){
			return obj.val
		}else{
			remove(USEOPENID);
			return ""
		}
	}else{
		remove(USEOPENID);
		return ""
	}
	
}
Storage.setRedPkg = (num)=>{
	set(REDPKG,num)
	// document.cookie = `${REDPKG}=${num};path=/`
}
Storage.getRedPkg = ()=>{
	// return getCookie(REDPKG)
	return Math.round(get(REDPKG))
}
Storage.setEditAddr =(obj)=>{
	return set(EDITADDR,obj)
}
Storage.getEditAddr =()=>{
	return get(EDITADDR)
}
Storage.setTip =()=>{
	return set(TIP,true)
}
Storage.getTip =()=>{
	return get(TIP)
}
Storage.getCookie = (name)=>{
	let arr,
		reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)) return parseInt(unescape(arr[2]));
	else return null;
}
Storage.getEndtime = () => {
	return get_endtime()
}
function set(k, v) {
	try {
		// #ifdef H5
			localStorage.setItem(k,JSON.stringify(v))
		// #endif
		// #ifndef H5
			uni.setStorageSync(k, v);
		// #endif
	} catch (e) {
	    // error
	}
	return;
}

function get(k) {
	try {
		// #ifdef H5
			return JSON.parse(localStorage.getItem(k));
		// #endif
		// #ifndef H5
			return uni.getStorageSync(k);
		// #endif
	} catch (e) {
	    console.log(e);
	}
}
function remove(k){
	try {
		// #ifdef H5
			localStorage.removeItem(k)
		// #endif
		// #ifndef H5
	    uni.removeStorageSync(k);
		// #endif
	} catch (e) {
	    // error
	}
}
function compare_time(time,day) {
	var today_end = get_endtime();
	var d = day?day:2;
	var time_old = time;
	if ((today_end - time_old) < d) {
	  return true
	} else {
	  return false
	}
}
function get_endtime() {
  var time_end = new Date();
  var time_format = format_date(time_end)
  return time_format;
}
function format_date(now) {
  var year = now.getFullYear(); //年
  var month = now.getMonth() + 1; //月
  var day = now.getDate(); //日
  var hh = now.getHours(); //时
  var mm = now.getMinutes(); //分
  var ss = now.getSeconds(); //秒

  var clock = year + "";
  if (month < 10) {
    clock += "0";
  }
  clock += month + "";

  if (day < 10) {
    clock += "0";
  }
  clock += day + "";
  return clock;
}

export default Storage